﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Project.dalSql;
using WISD.DBUtility.DBOperate;
using System.Collections;

namespace WISD.CarFriend.BLL.OA
{
    public class RechargeModule
    {
        #region MaticCode
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("RechargeID", "T_CF_OA_Recharge");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RechargeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_CF_OA_Recharge");
            strSql.Append(" where RechargeID=@RechargeID");
            SqlParameter[] parameters = {
					new SqlParameter("@RechargeID", SqlDbType.Int,4)
			};
            parameters[0].Value = RechargeID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_Recharge(");
            strSql.Append("MemberCardID,RechargeAmount,PaidAmount,Memo,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@MemberCardID,@RechargeAmount,@PaidAmount,@Memo,@Reviser,@ReviseDate,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberCardID", SqlDbType.NVarChar,50),
					new SqlParameter("@RechargeAmount", SqlDbType.Int,4),
					new SqlParameter("@PaidAmount", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.MemberCardID;
            parameters[1].Value = model.RechargeAmount;
            parameters[2].Value = model.PaidAmount;
            parameters[3].Value = model.Memo;
            parameters[4].Value = model.Reviser;
            parameters[5].Value = model.ReviseDate;
            parameters[6].Value = model.Memo1;
            parameters[7].Value = model.Memo2;
            parameters[8].Value = model.Memo3;
            parameters[9].Value = model.Memo4;
            parameters[10].Value = model.Memo5;
            parameters[11].Value = model.Memo6;
            parameters[12].Value = model.Memo7;
            parameters[13].Value = model.Memo8;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_CF_OA_Recharge set ");
            strSql.Append("MemberCardID=@MemberCardID,");
            strSql.Append("RechargeAmount=@RechargeAmount,");
            strSql.Append("PaidAmount=@PaidAmount,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("Reviser=@Reviser,");
            strSql.Append("ReviseDate=@ReviseDate,");
            strSql.Append("Memo1=@Memo1,");
            strSql.Append("Memo2=@Memo2,");
            strSql.Append("Memo3=@Memo3,");
            strSql.Append("Memo4=@Memo4,");
            strSql.Append("Memo5=@Memo5,");
            strSql.Append("Memo6=@Memo6,");
            strSql.Append("Memo7=@Memo7,");
            strSql.Append("Memo8=@Memo8");
            strSql.Append(" where RechargeID=@RechargeID");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberCardID", SqlDbType.NVarChar,50),
					new SqlParameter("@RechargeAmount", SqlDbType.Int,4),
					new SqlParameter("@PaidAmount", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100),
					new SqlParameter("@RechargeID", SqlDbType.Int,4)};
            parameters[0].Value = model.MemberCardID;
            parameters[1].Value = model.RechargeAmount;
            parameters[2].Value = model.PaidAmount;
            parameters[3].Value = model.Memo;
            parameters[4].Value = model.Reviser;
            parameters[5].Value = model.ReviseDate;
            parameters[6].Value = model.Memo1;
            parameters[7].Value = model.Memo2;
            parameters[8].Value = model.Memo3;
            parameters[9].Value = model.Memo4;
            parameters[10].Value = model.Memo5;
            parameters[11].Value = model.Memo6;
            parameters[12].Value = model.Memo7;
            parameters[13].Value = model.Memo8;
            parameters[14].Value = model.RechargeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RechargeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_Recharge ");
            strSql.Append(" where RechargeID=@RechargeID");
            SqlParameter[] parameters = {
					new SqlParameter("@RechargeID", SqlDbType.Int,4)
			};
            parameters[0].Value = RechargeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string RechargeIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_CF_OA_Recharge ");
            strSql.Append(" where RechargeID in (" + RechargeIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge GetModel(int RechargeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RechargeID,MemberCardID,RechargeAmount,PaidAmount,Memo,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 from T_CF_OA_Recharge ");
            strSql.Append(" where RechargeID=@RechargeID");
            SqlParameter[] parameters = {
					new SqlParameter("@RechargeID", SqlDbType.Int,4)
			};
            parameters[0].Value = RechargeID;

            WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge model = new WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RechargeID"] != null && ds.Tables[0].Rows[0]["RechargeID"].ToString() != "")
                {
                    model.RechargeID = int.Parse(ds.Tables[0].Rows[0]["RechargeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MemberCardID"] != null && ds.Tables[0].Rows[0]["MemberCardID"].ToString() != "")
                {
                    model.MemberCardID = ds.Tables[0].Rows[0]["MemberCardID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RechargeAmount"] != null && ds.Tables[0].Rows[0]["RechargeAmount"].ToString() != "")
                {
                    model.RechargeAmount = int.Parse(ds.Tables[0].Rows[0]["RechargeAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PaidAmount"] != null && ds.Tables[0].Rows[0]["PaidAmount"].ToString() != "")
                {
                    model.PaidAmount = int.Parse(ds.Tables[0].Rows[0]["PaidAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo"] != null && ds.Tables[0].Rows[0]["Memo"].ToString() != "")
                {
                    model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Reviser"] != null && ds.Tables[0].Rows[0]["Reviser"].ToString() != "")
                {
                    model.Reviser = ds.Tables[0].Rows[0]["Reviser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ReviseDate"] != null && ds.Tables[0].Rows[0]["ReviseDate"].ToString() != "")
                {
                    model.ReviseDate = DateTime.Parse(ds.Tables[0].Rows[0]["ReviseDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Memo1"] != null && ds.Tables[0].Rows[0]["Memo1"].ToString() != "")
                {
                    model.Memo1 = ds.Tables[0].Rows[0]["Memo1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo2"] != null && ds.Tables[0].Rows[0]["Memo2"].ToString() != "")
                {
                    model.Memo2 = ds.Tables[0].Rows[0]["Memo2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo3"] != null && ds.Tables[0].Rows[0]["Memo3"].ToString() != "")
                {
                    model.Memo3 = ds.Tables[0].Rows[0]["Memo3"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo4"] != null && ds.Tables[0].Rows[0]["Memo4"].ToString() != "")
                {
                    model.Memo4 = ds.Tables[0].Rows[0]["Memo4"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo5"] != null && ds.Tables[0].Rows[0]["Memo5"].ToString() != "")
                {
                    model.Memo5 = ds.Tables[0].Rows[0]["Memo5"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo6"] != null && ds.Tables[0].Rows[0]["Memo6"].ToString() != "")
                {
                    model.Memo6 = ds.Tables[0].Rows[0]["Memo6"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo7"] != null && ds.Tables[0].Rows[0]["Memo7"].ToString() != "")
                {
                    model.Memo7 = ds.Tables[0].Rows[0]["Memo7"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Memo8"] != null && ds.Tables[0].Rows[0]["Memo8"].ToString() != "")
                {
                    model.Memo8 = ds.Tables[0].Rows[0]["Memo8"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RechargeID,MemberCardID,RechargeAmount,PaidAmount,Memo,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_Recharge ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" RechargeID,MemberCardID,RechargeAmount,PaidAmount,Memo,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8 ");
            strSql.Append(" FROM T_CF_OA_Recharge ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_CF_OA_Recharge ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.RechargeID desc");
            }
            strSql.Append(")AS Row, T.*  from T_CF_OA_Recharge T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion


        #region SelfCode
        /// <summary>
        ///  初始化页面和查询数据
        /// </summary>
        /// <param name="pageSize">每页显示的条数</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="condition">搜索条件</param>
        /// <returns></returns>
        public static string GetData(int pageSize, int pageIndex, string condition)
        {
            StringBuilder tableName = new StringBuilder(); //从哪些表里面进行查询
            tableName.Append("  T_CF_OA_Recharge AS a ");
            string order = " a.RechargeID desc ";  //排序的字段
            StringBuilder fields = new StringBuilder(); //返回的列
            fields.Append(" a.RechargeID main, a.MemberCardID, a.RechargeAmount, a.PaidAmount, a.Reviser, a.ReviseDate  ");
            StringBuilder where = new StringBuilder(); //查询的条件
            where.Append(" 1=1 ");
            if (condition != "")
            {
                string[] array = condition.Split('@');
                string MemberCardID = array[0];
                string Reviser = array[1];

                if (MemberCardID.Trim() != "null")
                {
                    where.AppendFormat("  and a.MemberCardID=\'{0}\'   ", MemberCardID);
                }
                if (Reviser.Trim() != "null")
                {
                    where.AppendFormat("  and a.Reviser=\'{0}\'  ", Reviser);
                }
            }
            object[] objectArray = new object[] { tableName.ToString(), fields.ToString(), order, pageSize, pageIndex, where };
            DataTable dt = new DataTable();
            int recordCount = 0;
            dt = dalSqlHelper.ExecuteProcedure(objectArray, ref recordCount);
            if (dt.Rows.Count > 0)
            {
                return dalSqlHelper.DtToSON(dt) + "@" + recordCount.ToString();
            }
            else
            {
                return "@0";
            }
        }


        /// <summary>
        /// 查询指定主键对应的记录
        /// </summary>
        /// <param name="pk"></param>
        /// <returns></returns>
        public static string GetOne(string pk)
        {
            StringBuilder tableName = new StringBuilder(); //从哪些表里面进行查询
            tableName.Append(" T_CF_OA_Recharge a ");
            string order = " a.RechargeID desc ";  //排序的字段
            StringBuilder fields = new StringBuilder(); //返回的列
            fields.Append(" a.RechargeID,a.MemberCardID,a.RechargeAmount,a.PaidAmount,")
                .Append("a.Memo,a.Reviser,a.ReviseDate,a.Memo1,a.Memo2,a.Memo3,a.Memo4,a.Memo5,a.Memo6,a.Memo7,a.Memo8 ");
            StringBuilder where = new StringBuilder(); //查询的条件
            where.AppendFormat(" a.RechargeID={0} ", pk);
            object[] objectArray = new object[] { tableName.ToString(), fields.ToString(), order, 1, 1, where };
            DataTable dt = new DataTable();
            int recordCount = 0;
            dt = dalSqlHelper.ExecuteProcedure(objectArray, ref recordCount);
            if (dt.Rows.Count > 0)
            {
                return dalSqlHelper.DtToSON(dt);
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool AddEntity(WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge model)
        {
            bool result = false;

            try
            {
                MemberCardModule memberCardModule = new MemberCardModule();
                WISD.CarFriend.Model.CFModel.T_CF_OA_MemberCard memberCardModel
                    = memberCardModule.GetModel(model.MemberCardID);
                if (memberCardModel != null)
                {
                    int residualAmount = int.Parse(memberCardModel.ResidualAmount.ToString());    //剩余金额
                    DateTime expirationDate = DateTime.Parse(memberCardModel.ExpirationDate.ToString());    //截止日期
                    residualAmount += int.Parse(model.RechargeAmount.ToString());
                    if (DateTime.Compare(DateTime.Now, expirationDate) >= 0)
                    {
                        expirationDate = expirationDate.AddYears(1);
                    }

                    Hashtable sqlHash = new Hashtable();
                    Hashtable rechargeHash = GetSqlForAddRecharge(model);
                    Hashtable cardHash = GetSqlForUpdateMemberCard(residualAmount, expirationDate, model.MemberCardID);
                    sqlHash[rechargeHash["sql"]] = rechargeHash["parameters"];
                    sqlHash[cardHash["sql"]] = cardHash["parameters"];
                    DbHelperSQL.ExecuteSqlTranWithIndentity(sqlHash);

                    result = true;
                }
            }
            catch
            {
                result = false;
            }

            return result;
        }


        #region 内部方法
        /// <summary>
        /// 获取添加充值记录的SQL语句
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static Hashtable GetSqlForAddRecharge(WISD.CarFriend.Model.CFModel.T_CF_OA_Recharge model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_CF_OA_Recharge(");
            strSql.Append("MemberCardID,RechargeAmount,PaidAmount,Memo,Reviser,ReviseDate,Memo1,Memo2,Memo3,Memo4,Memo5,Memo6,Memo7,Memo8)");
            strSql.Append(" values (");
            strSql.Append("@MemberCardID,@RechargeAmount,@PaidAmount,@Memo,@Reviser,@ReviseDate,@Memo1,@Memo2,@Memo3,@Memo4,@Memo5,@Memo6,@Memo7,@Memo8)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberCardID", SqlDbType.NVarChar,50),
					new SqlParameter("@RechargeAmount", SqlDbType.Int,4),
					new SqlParameter("@PaidAmount", SqlDbType.Int,4),
					new SqlParameter("@Memo", SqlDbType.NVarChar,500),
					new SqlParameter("@Reviser", SqlDbType.NVarChar,50),
					new SqlParameter("@ReviseDate", SqlDbType.DateTime),
					new SqlParameter("@Memo1", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo2", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo3", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo4", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo5", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo6", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo7", SqlDbType.NVarChar,100),
					new SqlParameter("@Memo8", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.MemberCardID;
            parameters[1].Value = model.RechargeAmount;
            parameters[2].Value = model.PaidAmount;
            parameters[3].Value = model.Memo;
            parameters[4].Value = model.Reviser;
            parameters[5].Value = model.ReviseDate;
            parameters[6].Value = model.Memo1;
            parameters[7].Value = model.Memo2;
            parameters[8].Value = model.Memo3;
            parameters[9].Value = model.Memo4;
            parameters[10].Value = model.Memo5;
            parameters[11].Value = model.Memo6;
            parameters[12].Value = model.Memo7;
            parameters[13].Value = model.Memo8;

            Hashtable ht = new Hashtable();
            ht["sql"] = strSql.ToString();
            ht["parameters"] = parameters;
            return ht;
        }


        /// <summary>
        /// 获取修改剩余金额、到期日期的SQL语句
        /// </summary>
        /// <param name="residualAmount"></param>
        /// <param name="expirationDate"></param>
        /// <param name="memberCardID"></param>
        /// <returns></returns>
        private static Hashtable GetSqlForUpdateMemberCard(int residualAmount, DateTime expirationDate, string memberCardID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" UPDATE  T_CF_OA_MemberCard ");
            strSql.Append(" SET  ResidualAmount = @ResidualAmount, ExpirationDate = @ExpirationDate ");
            strSql.Append(" WHERE  MemberCardID = @MemberCardID ");
            SqlParameter[] parameters = {
                    new SqlParameter("@ResidualAmount", SqlDbType.Int,4),
                    new SqlParameter("@ExpirationDate", SqlDbType.DateTime),
					new SqlParameter("@MemberCardID", SqlDbType.NVarChar,50)};
            parameters[0].Value = residualAmount;
            parameters[1].Value = expirationDate;
            parameters[2].Value = memberCardID;

            Hashtable ht = new Hashtable();
            ht["sql"] = strSql.ToString();
            ht["parameters"] = parameters;
            return ht;
        }

        #endregion

        #endregion

    }
}
